<?php if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

class Collectiondetails extends CI_Controller {

	public function __construct() {
		parent::__construct();
		if (php_sapi_name() === 'cli') {
			$this->load->model('ub/collectiondetails_model');
		} else {
			$this->load->model($this->session->userdata('formodel'));
		}
		$this->load->library('variable');
		$this->load->library('PHPexcel');
		$this->load->helper('download');
		$this->load->helper('file');

	}
	public function index() {
		$this->load->view('layouts/header', $this->variable->load_data($this->session->userdata('title')));
		$this->load->view($this->session->userdata('formain'));
		$this->load->view('layouts/footer');
	}

	public function get_status() {
		$r1 = $this->input->post('range1');
		$r2 = $this->input->post('range2');
		$d1 = $this->input->post('date_1');
		$d2 = $this->input->post('date_2');
		$ref1 = $this->input->post('ref1');
		$ref2 = $this->input->post('ref2');
		$query = $this->collectiondetails_model->get_status($r1,$r2,$d1,$d2,$ref1,$ref2);
		echo json_encode($query);
	}

	public function mail_window() {
		$this->load->view($this->session->userdata('formain') . '_mail', array('title' => "Email Form"));
	}

	public function send_mail() {

		// $ci = get_instance();

		$config['protocol'] = "smtp";
		$config['smtp_host'] = "ssl://smtp.gmail.com";
		$config['mailpath'] = "assets/tmp";
		$config['smtp_port'] = 465;
		$config['smtp_user'] = "tetsuyakuroko101@gmail.com";
		$config['smtp_pass'] = "dwuydqsvumopnxdk";
		$config['smtp_timeout'] = 5;
		$config['charset'] = "utf-8";
		$config['mailtype'] = "html";
		$config['newline'] = "\r\n";
		$config['crlf'] = "\n";

		$this->email->initialize($config);

		$attachment_excel;
		$attachment_pdf;

		$this->email->from($this->input->post('email_from'), $this->input->post('email_from'));
		$list = array($this->input->post('email_to'));
		$this->email->to($this->input->post('email_to'));
		// $this->email->reply_to('my-email@gmail.com', 'Explendid Videos');
		$this->email->subject($this->input->post('email_subj'));
		// $msg = $this->input->post('email_message') == '' ? ' ' : $this->input->post('email_message');
		$this->email->message($this->input->post('email_message'));
		if (!empty($this->input->post('email_toexcel'))) {
			$attachment_excel = $this->gen_excel_mail($this->input->post('range1'), $this->input->post('range2'),$this->input->post('date_1'),$this->input->post('date_2'), $this->input->post('email_filename'));
			$this->email->attach($attachment_excel);
		}
		if (!empty($this->input->post('email_topdf'))) {
			$attachment_pdf = $this->gen_pdf_mail($this->input->post('range1'), $this->input->post('range2'),$this->input->post('date_1'),$this->input->post('date_2'), $this->input->post('email_filename'));
			$this->email->attach($attachment_pdf);
		}
		// $attachment_pdf = $this->gen_pdf_mail($this->input->post('range1'), $this->input->post('range2'), $this->input->post('email_filename'));
		// $this->email->attach($attachment_pdf);
		if ($this->email->send()) {
			echo json_encode(array('msg' => 'success'));
			if (!empty($attachment_pdf)) {
				unlink(realpath($attachment_pdf));
			}
			if (!empty($attachment_excel)) {
				unlink(realpath($attachment_excel));
			}
		} else {
			echo json_encode(array('msg' => 'fail'));
			show_error($this->email->print_debugger());
			if (!empty($attachment_pdf)) {
				unlink(realpath($attachment_pdf));
			}
			if (!empty($attachment_excel)) {
				unlink(realpath($attachment_excel));
			}
		}

	}

	public function gen_pdf() {
		$result = $this->collectiondetails_model->get_status($_GET['range1'], $_GET['range2'],$_GET['date_1'],$_GET['date_2'],$_GET['ref_1'],$_GET['ref_2']);
		//var_dump($result);
		$table_data = '';
		//<td style='border-bottom:1px solid; text-align: center;width=8%; padding: 5px;'>Year</td>
		//<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . $key->year . '</td>
		foreach ($result as $key) {
			$table_data .= '<tr><td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;">' . $key->receipt_date . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . $key->receipt_number . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;">' . $key->acct_name . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="center">' . $key->status . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . $key->pay_amount . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . $key->reference . '</td></tr>';
		}
		$content = "
		<page>
        	<table style='width: 100%; padding-top:-10px;'>
	            <tr>
			        <td style='text-align: center;width=33%;'><hr style='height: .3mm; background: #000; '></td>
			        <td style='text-align: center;width=33%; padding-top:-5px;'>
						<h4>Receipt Range From " . $_GET['range1'] . " to " . $_GET['range2'] . "</h4>
					</td>
					<td style='text-align: center;width=33%;'><hr style='height: .3mm; background: #000; '>     </td>
				</tr>
				 <tr>
			        <td style='text-align: center;width=33%;'><hr style='height: .3mm; background: #000; '></td>
			        <td style='text-align: center;width=33%; padding-top:-5px;'>
						<h4>Payment Date From " . $_GET['date_1'] . " to " . $_GET['date_2'] . "</h4>
					</td>
					<td style='text-align: center;width=33%;'><hr style='height: .3mm; background: #000; '>     </td>
				</tr>
			</table><br><br>
			<table cellspacing='0' style='width: 100%; position: relative;'>
				<tr>
					<td style='border-bottom:1px solid; text-align: center;width=16%; padding: 5px;'>Date</td>
					<td style='border-bottom:1px solid; text-align: center;width=13%; padding: 5px;'>Receipt Number</td>
					<td style='border-bottom:1px solid; text-align: center;width=29%; padding: 5px;'>Account Name</td>
					<td style='border-bottom:1px solid; text-align: center;width=14%; padding: 5px;'>Status</td>
					<td style='border-bottom:1px solid; text-align: center;width=14%; padding: 5px;'>Bill Details</td>
					<td style='border-bottom:1px solid; text-align: center;width=14%; padding: 5px;'>Reference</td>
				</tr>" .
			$table_data
			. "</table>
		</page>
		";
		require_once dirname(__FILE__) . '\..\..\..\assets\html2pdf\html2pdf.class.php';
		$html2pdf = new HTML2PDF('P', 'A4', 'en');
		$html2pdf->pdf->SetTitle('Collection Details Report');
		$html2pdf->WriteHTML($content);
		$html2pdf->Output('Collection_Details_' . date("dmY_His") . '.pdf', 'D');
	}

	public function gen_excel() {
		$range1 = 'null';
		$range2 = 'null';
		$d1 = 'null';
		$d2 ='null';
		if (!empty($_GET['range1'])) {
			$range1 = $_GET['range1'];
		}
		if (!empty($_GET['range2'])) {
			$range2 = $_GET['range2'];
		}
		if (!empty($_GET['date_1'])) {
			$d1 = $_GET['date_1'];
		}
		if (!empty($_GET['date_2'])) {
			$d2 = $_GET['date_2'];
		}
		//activate worksheet number 1
		$excel = new PHPExcel();
		$excel->setActiveSheetIndex(0);
		//name the worksheet
		$excel->getActiveSheet()->setTitle('Collection Details Report');
		//set cell A1 content with some text
		$excel->getActiveSheet()->setCellValue('A1', 'Receipt Range From ' . $range1 . ' to ' . $range2 .' AND Payment Date From '.$d1.' to '.$d2);
		$excel->getActiveSheet()->setCellValue('A3', 'Date');
		$excel->getActiveSheet()->setCellValue('B3', 'Receipt Number');
		$excel->getActiveSheet()->setCellValue('C3', 'Account Name');
		$excel->getActiveSheet()->setCellValue('D3', 'Status');
		$excel->getActiveSheet()->setCellValue('E3', 'Year');
		$excel->getActiveSheet()->setCellValue('F3', 'Bill Details');
		$excel->getActiveSheet()->setCellValue('G3', 'Reference');

		$result = $this->collectiondetails_model->get_status($_GET['range1'],$_GET['range2'],$_GET['date_1'],$_GET['date_2'],$_GET['ref_1'],$_GET['ref_2']);
		$cellnum = 4;

		foreach ($result as $key) {
			$excel->getActiveSheet()->setCellValue('A' . $cellnum, $key->receipt_date);
			$excel->getActiveSheet()->setCellValue('B' . $cellnum, $key->receipt_number);
			$excel->getActiveSheet()->setCellValue('C' . $cellnum, $key->acct_name);
			$excel->getActiveSheet()->setCellValue('D' . $cellnum, $key->status);
			$excel->getActiveSheet()->setCellValue('E' . $cellnum, $key->year);
			$excel->getActiveSheet()->setCellValue('F' . $cellnum, $key->pay_amount);
			$excel->getActiveSheet()->getStyle('F' . $cellnum)->getNumberFormat()->setFormatCode('###,###,###.00');
			$excel->getActiveSheet()->setCellValue('G' . $cellnum, $key->reference);
			$cellnum++;
		}
		//$excel->getActiveSheet()->getStyle('A1:F' . $cellnum = $cellnum - 1)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

		// $excel->getActiveSheet()->getStyle('A1:F1')->getFill()->setFillType(PHPexcel_Style_Fill::FILL_SOLID);
		//       $excel->getActiveSheet()->getStyle('A1:F1')->getFill()->getStartColor()->setARGB('29bb04');
		//       // Add some data
		//       $excel->getActiveSheet()->getStyle("A1:F1")->getFont()->setBold(true);
		//       $excel->getActiveSheet()->getStyle('A1:F1')->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		//$excel->getActiveSheet()->getStyle('A2:F2')->getFill()->setFillType(PHPexcel_Style_Fill::FILL_SOLID);
		//$excel->getActiveSheet()->getStyle('A2:F2')->getFill()->getStartColor()->setARGB('6F6F6F');
		// Add some data
		$excel->getActiveSheet()->getStyle("A3:G3")->getFont()->setBold(true);
		//$excel->getActiveSheet()->getStyle('A2:F2')->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$excel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('C')->setAutoSize(false);
		$excel->getActiveSheet()->getColumnDimension('C')->setWidth(40);
		$excel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
		$excel->getActiveSheet()->getRowDimension('1')->setRowHeight(25);
		//change the font size
		$excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(12);
		//make the font become bold
		$excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		//merge cell A1 until D1
		$excel->getActiveSheet()->mergeCells('A1:F1');
		//set aligment to center for that merged cell (A1 to D1)
		$excel->getActiveSheet()->getStyle('A3:G3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$excel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
		//$excel->getActiveSheet()->getStyle("A2:F2")->getFont()->getColor()->setRGB('FFFFFF');

		$excel->getActiveSheet()->setShowGridlines(false);

		$filename = 'Collection_Details_Report_ ' . date('Ymd_his') . '.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="' . $filename . '"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel5');
		$objWriter->save('php://output');
		//force user to download the Excel file without writing it to server's HD
		//$objWriter->save("assets/tmp/" . $filename);
		//$this->index();
		// $data = ob_get_contents();
		//$tester =  $objWriter->save($filename);
		//force_download($filename,$objWriter->save('php://output')));
	}

	public function gen_pdf_mail($range_1, $range_2,$d1,$d2, $file_name) {
		$result = $this->collectiondetails_model->get_status($range_1, $range_2,$d1,$d2);
		$table_data = '';
		// /<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . $key->year . '</td>
		//<td style='border-bottom:1px solid; text-align: center;width=8%; padding: 5px;'>Year</td>
		foreach ($result as $key) {
			$table_data .= '<tr><td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;">' . $key->receipt_date . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . $key->receipt_number . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;">' . $key->acct_name . '</td>
								<td align="center" style="padding-right: 5px; padding-left: 5px; padding-top: 5px;">' . $key->status . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . $key->pay_amount . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . $key->reference . '</td></tr>';
		}
		$content = "
		<page>
        	<table style='width: 100%; padding-top:-10px;'>
	            <tr>
			        <td style='text-align: center;width=33%;'><hr style='height: .3mm; background: #000; '></td>
			        <td style='text-align: center;width=33%; padding-top:-5px;'>
						<h4>Receipt Range From " . $range_1 . " to " . $range_2 . "</h4>
					</td>
					<td style='text-align: center;width=33%;'><hr style='height: .3mm; background: #000; '>     </td>
				</tr>
				<tr>
			        <td style='text-align: center;width=33%;'><hr style='height: .3mm; background: #000; '></td>
			        <td style='text-align: center;width=33%; padding-top:-5px;'>
						<h4>Payment Date From " . $d1 . " to " . $d2 . "</h4>
					</td>
					<td style='text-align: center;width=33%;'><hr style='height: .3mm; background: #000; '>     </td>
				</tr>
			</table><br><br>
			<table cellspacing='0' style='width: 100%; position: relative;'>
				<tr>
					<td style='border-bottom:1px solid; text-align: center;width=16%; padding: 5px;'>Date</td>
					<td style='border-bottom:1px solid; text-align: center;width=13%; padding: 5px;'>Receipt Number</td>
					<td style='border-bottom:1px solid; text-align: center;width=29%; padding: 5px;'>Account Name</td>
					<td style='border-bottom:1px solid; text-align: center;width=17%; padding: 5px;'>Status</td>
					
					<td style='border-bottom:1px solid; text-align: center;width=17%; padding: 5px;'>Bill Details</td>
					<td style='border-bottom:1px solid; text-align: center;width=17%; padding: 5px;'>Reference</td>
				</tr>" .
			$table_data
			. "</table>
		</page>
		";
		require_once dirname(__FILE__) . '\..\..\..\assets\html2pdf\html2pdf.class.php';
		$html2pdf = new HTML2PDF('P', 'A4', 'en');
		$html2pdf->pdf->SetTitle('Collection Details: Receipt Range From ' . $range_1 . ' to ' . $range_2);
		$html2pdf->WriteHTML($content);
		//$html2pdf->Output('Collection_Details_' . date("dmY_His") . '.pdf', 'D');
		$html2pdf->Output('assets/tmp/' . $file_name . '.pdf', 'F');
		$data = 'assets/tmp/' . $file_name . '.pdf';
		return $data;
	}

	public function gen_excel_mail($range_1, $range_2,$d1,$d2, $file_name) {
		$range1 = 'null';
		$range2 = 'null';
		$d1 = 'null';
		$d2 = 'null';
		if (!empty($_GET['range1'])) {
			$range1 = $_GET['range1'];
		}
		if (!empty($_GET['range2'])) {
			$range2 = $_GET['range2'];
		}
		if (!empty($_GET['date_1'])) {
			$d1 = $_GET['date_1'];
		}
		if (!empty($_GET['date_2'])) {
			$d2 = $_GET['date_2'];
		}
		//activate worksheet number 1
		$excel = new PHPExcel();
		$excel->setActiveSheetIndex(0);
		//name the worksheet
		$excel->getActiveSheet()->setTitle('test worksheet');
		//set cell A1 content with some texta
		$excel->getActiveSheet()->setCellValue('A1', 'Range From ' . $range1 . ' to ' . $range2 .' AND Payment Date From '.$d1.' to '.$d2);
		$excel->getActiveSheet()->setCellValue('A3', 'Date');
		$excel->getActiveSheet()->setCellValue('B3', 'Receipt Number');
		$excel->getActiveSheet()->setCellValue('C3', 'Account Name');
		$excel->getActiveSheet()->setCellValue('D3', 'Status');
		$excel->getActiveSheet()->setCellValue('E3', 'Year');
		$excel->getActiveSheet()->setCellValue('F3', 'Bill Details');
		$excel->getActiveSheet()->setCellValue('G3', 'Reference');
		$result = $this->collectiondetails_model->get_status($range_1, $range_2,$d1,$d2);
		$cellnum = 4;

		foreach ($result as $key) {
			$excel->getActiveSheet()->setCellValue('A' . $cellnum, $key->receipt_date);
			$excel->getActiveSheet()->setCellValue('B' . $cellnum, $key->receipt_number);
			$excel->getActiveSheet()->setCellValue('C' . $cellnum, $key->acct_name);
			$excel->getActiveSheet()->setCellValue('D' . $cellnum, $key->status);
			$excel->getActiveSheet()->setCellValue('E' . $cellnum, $key->year);
			$excel->getActiveSheet()->setCellValue('F' . $cellnum, $key->pay_amount);
			$excel->getActiveSheet()->getStyle('F' . $cellnum)->getNumberFormat()->setFormatCode('###,###,###.00');
			$excel->getActiveSheet()->setCellValue('G' . $cellnum, $key->reference);
			$cellnum++;
		}
		//$excel->getActiveSheet()->getStyle('A1:F' . $cellnum = $cellnum - 1)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

		// $excel->getActiveSheet()->getStyle('A1:F1')->getFill()->setFillType(PHPexcel_Style_Fill::FILL_SOLID);
		//       $excel->getActiveSheet()->getStyle('A1:F1')->getFill()->getStartColor()->setARGB('29bb04');
		//       // Add some data
		//       $excel->getActiveSheet()->getStyle("A1:F1")->getFont()->setBold(true);
		//       $excel->getActiveSheet()->getStyle('A1:F1')->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		//$excel->getActiveSheet()->getStyle('A2:F2')->getFill()->setFillType(PHPexcel_Style_Fill::FILL_SOLID);
		//$excel->getActiveSheet()->getStyle('A2:F2')->getFill()->getStartColor()->setARGB('6F6F6F');
		// Add some data
		$excel->getActiveSheet()->getStyle("A3:G3")->getFont()->setBold(true);
		//$excel->getActiveSheet()->getStyle('A2:F2')->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$excel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('C')->setAutoSize(false);
		$excel->getActiveSheet()->getColumnDimension('C')->setWidth(40);
		$excel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
		$excel->getActiveSheet()->getRowDimension('1')->setRowHeight(25);
		//change the font size
		$excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(12);
		//make the font become bold
		$excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		//merge cell A1 until D1
		$excel->getActiveSheet()->mergeCells('A1:F1');
		//set aligment to center for that merged cell (A1 to D1)
		$excel->getActiveSheet()->getStyle('A2:G2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$excel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
		//$excel->getActiveSheet()->getStyle("A2:F2")->getFont()->getColor()->setRGB('FFFFFF');
		$excel->getActiveSheet()->setShowGridlines(false);
		$filename = 'Collection_Details_Report_ ' . date('Ymd_his') . '.xls'; //save our workbook as this file name
		// header('Content-Type: application/vnd.ms-excel'); //mime type
		// header('Content-Disposition: attachment;filename="' . $file_name . '"'); //tell browser what's the file name
		// header('Cache-Control: max-age=0'); //no cache
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel5');
		//force user to download the Excel file without writing it to server's HD
		// $objWriter->save(dirname(__FILE__) . "/../../../assets/tmp");
		// $data = ob_get_contents();
		$objWriter->save("assets/tmp/" . $file_name . '.xls');
		$data = "assets/tmp/" . $file_name . '.xls';
		return $data;
		//$tester =  $objWriter->save($filename);
		//force_download($filename,$objWriter->save('php://output')));
	}

	public function mail_sched($email_to, $email_type) {
		//$email_to = 'boygee2025@gmail.com';
		$email_from = 'no-reply@ums.com';
		$email_subj = $email_type . ' Report (' . date('Y-m-d') . ')';
		$email_filename = $email_type . ' Report (' . date('Y-m-d') . ')';
		$range1 = 1;
		$range2 = 3;

		$config['protocol'] = "smtp";
		$config['smtp_host'] = "ssl://smtp.gmail.com";
		$config['mailpath'] = "assets/tmp";
		$config['smtp_port'] = 465;
		$config['smtp_user'] = "tetsuyakuroko101@gmail.com";
		$config['smtp_pass'] = "dwuydqsvumopnxdk";
		$config['smtp_timeout'] = 5;
		$config['charset'] = "utf-8";
		$config['mailtype'] = "html";
		$config['newline'] = "\r\n";
		$config['crlf'] = "\n";

		$this->email->initialize($config);

		$this->email->from($email_from, $email_from);
		$this->email->to($email_to);
		$this->email->subject($email_subj);

		$attachment_excel = $this->gen_excel_mail($range1, $range2, $email_filename);
		$this->email->attach($attachment_excel);
		$attachment_pdf = $this->gen_pdf_mail($range1, $range2, $email_filename);
		$this->email->attach($attachment_pdf);
		if ($this->email->send()) {
			if (!empty($attachment_pdf)) {
				unlink(realpath($attachment_pdf));
			}
			if (!empty($attachment_excel)) {
				unlink(realpath($attachment_excel));
			}
			//echo "Success" . PHP_EOL;
			return true;
		} else {
			show_error($this->email->print_debugger());
			if (!empty($attachment_pdf)) {
				unlink(realpath($attachment_pdf));
			}
			if (!empty($attachment_excel)) {
				unlink(realpath($attachment_excel));
			}
			//echo "Fail" . PHP_EOL;
			return false;
		}
		//echo "Hello {$range1} {$range2}!" . PHP_EOL;
	}

	public function check_monthly() {
		$query = $this->collectiondetails_model->get_email('Monthly');
		foreach ($query as $key) {
			if ($key->mail_interval_day == date("d") && $key->is_sent == 0) {
				$max = 3;
				for ($i = 1; $i <= $max; $i++) {
					if ($this->mail_sched($key->email_address, 'Monthly') == true) {
						$this->collectiondetails_model->update_sentemail($key->email_id);
						break;
					}
				}
			}
		}
	}
	public function check_daily() {
		$query = $this->collectiondetails_model->get_email('Daily');
		foreach ($query as $key) {
			$time = explode(':', $key->mail_interval_time);
			if (implode(':', array($time[0], $time[1])) == date("H:i") && $key->is_sent == 0) {
				$max = 3;
				for ($i = 1; $i <= $max; $i++) {
					if ($this->mail_sched($key->email_address, 'Daily') == true) {
						$this->collectiondetails_model->update_sentemail($key->email_id);
						break;
					}
				}
			}
		}
	}

	public function mail_reset() {

	}

	public function testing() {
		$output = shell_exec('start /b C:\xampp\htdocs\ums\task\mail.bat');
		echo $output;
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */